package cn.edu.gdgm.system.controller;

import cn.edu.gdgm.system.dto.Result;
import cn.edu.gdgm.system.entity.Plan;
import cn.edu.gdgm.system.service.IPlanService;
import cn.fightingguys.security.web.wechat.entity.WeChatMiniProgramUserDetails;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

/**
 * <p>
 * 服药计划 前端控制器
 * </p>
 *
 * @author generator
 * @since 2021-02-26
 */
@Api(tags = {"服药计划Controller"})
@RestController
@RequestMapping("/plan")
public class PlanController {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Resource
    private IPlanService planService;


    @ApiOperation(value = "新增服药计划")
    @PreAuthorize("hasAuthority('Verified')")
    @PostMapping()
    public Result add(Plan plan, @ApiIgnore @AuthenticationPrincipal WeChatMiniProgramUserDetails userDetails) {
        try {
            String openId = userDetails.getOpenId();
            plan.setUserId(openId);
            return toResult(planService.add(plan));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(500, "服务器异常");
        }
    }

    @ApiOperation(value = "删除服药计划")
    @DeleteMapping("{id}")
    @PreAuthorize("hasAuthority('Verified')")
    public Result delete(@PathVariable("id") Long id) {
        try {
            return toResult(planService.delete(id));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(500, "服务器错误");
        }
    }

    @ApiOperation(value = "更新服药计划")
    @PutMapping()
    @PreAuthorize("hasAuthority('Verified')")
    public Result update(Plan plan) {
        try {
            return toResult(planService.updateData(plan));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(500, "服务器错误");
        }
    }

    @ApiOperation(value = "查询服药计划数据")
    @PreAuthorize("hasAuthority('Verified')")
    @GetMapping()
    public Result list(@ApiIgnore @AuthenticationPrincipal WeChatMiniProgramUserDetails userDetails) {
        try {
            return Result.success(planService.list(new QueryWrapper<Plan>().eq("user_id", userDetails.getOpenId())));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(500, "服务器错误");
        }
    }

    @ApiOperation(value = "id查询服药计划")
    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('Verified')")
    public Result findById(@PathVariable Long id) {
        try {
            return Result.success(planService.findById(id));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(500, "服务器错误");
        }
    }


    @ApiOperation(value = "暂停服药计划")
    @PutMapping("/pause")
    @PreAuthorize("hasAuthority('Verified')")
    public Result pause(Long id) {
        return toResult(planService.pause(id));
    }

    @ApiOperation(value = "恢复服药计划")
    @PutMapping("/start")
    @PreAuthorize("hasAuthority('Verified')")
    public Result start(Long id) {
        return toResult(planService.start(id));
    }

    /**
     * 封装逻辑业务状态
     *
     * @param i 数据库影响结果
     * @return Result
     */
    private Result toResult(int i) {
        if (i > 0) {
            return Result.success("操作成功");
        } else {
            return Result.error(201, "操作失败");
        }
    }

}
